#!/usr/bin/perl
#index.cgi

require './globalnet-lib.pl';

&ReadParse();
if ($in{'mode'} ne "cache")
{
	$dns = &get_dns_config();
	&read_env_file($config{'network_config'}, \%conf);
	my $gatewaydev = ($conf{'GATEWAYDEV'})?$conf{'GATEWAYDEV'}:"eth0";
	&read_env_file("/etc/sysconfig/network-scripts/ifcfg-".$gatewaydev, \%confi);
	$in{'gatewayin'} = &get_gateway_default();
        $in{'gw2'}=&get_gw2();
	$in{'dnscache'} = join(":",@{$dns->{'nameserver'}});
	$in{'domaincache'} = join(":",@{$dns->{'domain'}});
	@hostlist = &list_hosts();
	$in{'hostscache'} = join(":", @hostlist);
}
	my $bonding = "off";
 	local (@lref,$line);
 	open(LSMOD, "lsmod |");
 	while(<LSMOD>) {
 		s/\r|\n//g;
 		if (/^\S+/) { push(@lref, $_); }
                else { $lref[$#lref] .= $_; }
        }
        close(LSMOD);

        foreach $line (@lref)
        {
                if($line=~/bonding/)
                {
                        $bonding = "on";
                        last;
                }
        }

&header($text{'global_title'}, "", "index", 0, 1);
#print "<hr>\n";
print "<form action=save_global.cgi method=post onsubmit='disable_buttons();'>\n";
print "<input type=hidden name=gatewaydev value='$conf{'GATEWAYDEV'}'>\n";
print "<input type=hidden name=dnscache value='$in{'dnscache'}'>\n";
print "<input type=hidden name=domaincache value='$in{'domaincache'}'>\n";
print "<input type=hidden name=hostscache value='$in{'hostscache'}'>\n";
print "<input type=hidden name=mode value='cache'>\n";

print "<table class=mode1 border=0 cellpadding=5 width=100%>\n";
print "<tr> <td><b>$text{'gateway'}</b></td>\n";
print "<td colspan=1><input name=gatewayin size=30 class=text value='$in{'gatewayin'}'></td><td><b>$text{'gateway2'}</b></td><td><input name=gw2 size=30 class=text value='$in{'gw2'}'></td></tr>\n";
#printf "<td colspan=5><input name=gatewayin size=30 class=text value='$in{'gatewayin'}' %s></td></tr>\n",(($confi{'BOOTPROTO'} eq "static") && ($bonding eq "off"))?"":"disabled";
print "<tr> <td valign=top><b>$text{'dns_server'}</b></td>\n";
@dnslist = split(":", $in{'dnscache'});
print "<td><select class=list name=dnssel size=7>\n";
for ($i = 0 ;$i < @dnslist; $i++)
{
	print "<option value='$dnslist[$i]'>$dnslist[$i]\n";
}
print "</select></td>\n";
print "<td><table border=0 class=mode1>\n";
print "<tr><td><input class=button1 type=button name=adddnsbtn value='$text{'add'}' onclick='add_dns()'></td></tr>\n";
print "<tr><td><input class=button1 type=button name=deldnsbtn value='$text{'remove'}' onclick='remove_dns()'></td></tr>\n";
print "<tr><td><input class=button1 type=button name=updnsbtn value='$text{'up'}' onclick='up_dns()'></td></tr>\n";
print "<tr><td><input class=button1 type=button name=downdnsbtn value='$text{'down'}' onclick='down_dns()'></td></tr>\n";
print "</table></td>\n";

#print "<td valign=top><b>$text{'search_domain'}</b></td>\n";
@domainlist = split(":", $in{'domaincache'});
#print "<td><select class=list name=domainsel size=7>\n";
#for ($i = 0 ;$i < @domainlist; $i++)
#{
#	print "<option value='$domainlist[$i]'>$domainlist[$i]\n";
#}
#print "</select></td>\n";
#print "<td><table border=0 class=mode1>\n";
#print "<tr><td><input class=button1 type=button name=adddomainbtn value='$text{'add'}' onclick='add_domain()'></td></tr>\n";
#print "<tr><td><input class=button1 type=button name=deldomainbtn value='$text{'remove'}' onclick='remove_domain()'></td></tr>\n";
#print "<tr><td><input class=button1 type=button name=updomainbtn value='$text{'up'}' onclick='up_domain()'></td></tr>\n";
#print "<tr><td><input class=button1 type=button name=downdomainbtn value='$text{'down'}' onclick='down_domain()'></td></tr>\n";
#print "</table></td>";
print "</tr>\n";


print "<tr><td valign=top><b>$text{'tcpip_hosts'}</b></td>\n";
print "<td colspan=3><div class=small><table class=mode1 cellpadding=2 cellspacing=0 width=100%>\n";
print "<tr class=title1><td><b>$text{'ip_address'}</b></td>\n";
print "<td><b>$text{'host_name'}</b></td></tr>\n";
@hosts = split(":", $in{'hostscache'});
$n = @hosts;
for ($i = 0 ; $i < @hosts; $i ++)
{
	if ($i %2)
	{
		$dismode = "class=mode2";
	}
	else
	{
		$dismode = "class=mode1";
	}
	$hosts[$i] =~ /^(.*)=(.*)$/;
	print "<tr $dismode><td valign=top><input type=radio name=hostrdn><input class=text name=ipin value='$1' onchange='cachehostlist($n)'></td>\n";
	print "<td valign=top><input class=text size=25 name=hostin value='$2' onchange='cachehostlist($n)'></td></tr>\n";
}
print "</table></div></td>\n";
print "<td valign=top colspan=2><table class=mode1 cellpadding=2 cellspacing=0 width=100%>\n";
print "<tr><td><b>$text{'ip_address'}</b></td>\n";
print "<td><input name=ipaddin class=text size=25></td></tr>\n";
print "<tr><td><b>$text{'host_name'}</b></td>\n";
print "<td><input name=hostaddin class=text size=25></td></tr>\n";
print "<tr><td><input type=button class=button1 name=addhost value='$text{'add'}' onclick='add_host($n)'></td>\n";
print "<td><input type=button class=button1 name=delhost value='$text{'remove'}' onclick='remove_host($n)'></td></tr>\n";
print "</table></td> </tr>\n";
print "<tr><td colspan=2><input type=submit name=savebtn class=button1 value='$text{'submit'}' onclick='refresh_submit()'></td></tr>\n";
print "</table></form>\n";
&nas_syslog("globalnet", "naslog_info", 17000, "Get global network settings");
print <<EOF;
<script>
function add_dns()
{
	var tmp = prompt("$text{'add_dns_msg'}","");
	if (tmp)
	{
		i = document.forms[0].dnssel.options.length;
		document.forms[0].dnssel.options[i] = new Option(tmp,tmp);
		refresh_dnscache();
	}
}

function remove_dns()
{
	var idx = document.forms[0].dnssel.selectedIndex;

	if ( idx >= 0)
	{

		document.forms[0].dnssel.options[idx] = null;
		refresh_dnscache();
	}
}

function up_dns()
{
	var idx = document.forms[0].dnssel.selectedIndex;

	if ( idx >= 1)
	{
		t_value = document.forms[0].dnssel.options[idx].value;
		document.forms[0].dnssel.options[idx].value = document.forms[0].dnssel.options[idx - 1].value;
		document.forms[0].dnssel.options[idx].text = document.forms[0].dnssel.options[idx - 1].text;
		document.forms[0].dnssel.options[idx - 1].value = t_value;
		document.forms[0].dnssel.options[idx - 1].text = t_value;
		document.forms[0].dnssel.selectedIndex --;
		refresh_dnscache();
	}
}

function down_dns()
{
	var idx = document.forms[0].dnssel.selectedIndex;

	if ( (idx < document.forms[0].dnssel.options.length - 1)&&(idx >=0))
	{
		t_value = document.forms[0].dnssel.options[idx].value;
		document.forms[0].dnssel.options[idx].value = document.forms[0].dnssel.options[idx + 1].value;
		document.forms[0].dnssel.options[idx].text = document.forms[0].dnssel.options[idx + 1].text;
		document.forms[0].dnssel.options[idx + 1].value = t_value;
		document.forms[0].dnssel.options[idx + 1].text = t_value;
		document.forms[0].dnssel.selectedIndex ++;
		refresh_dnscache();
	}
}

function refresh_dnscache()
{
	var str ="";
	for (i = 0; i < document.forms[0].dnssel.options.length; i++)
	{
		str += (document.forms[0].dnssel.options[i].value + ":");
		//if (i < (document.forms[0].hostlist.options.length -1))
		//	str +=":";
	}
	document.forms[0].dnscache.value = str;
}

function add_domain()
{
	var tmp = prompt("$text{'add_domain_msg'}","");
	if (tmp)
	{
		i = document.forms[0].domainsel.options.length;
		document.forms[0].domainsel.options[i] = new Option(tmp,tmp);
		refresh_domaincache();
	}
}

function remove_domain()
{
	var idx = document.forms[0].domainsel.selectedIndex;

	if ( idx >= 0)
	{

		document.forms[0].domainsel.options[idx] = null;
		refresh_domaincache();
	}
}

function up_domain()
{
	var idx = document.forms[0].domainsel.selectedIndex;

	if ( idx >= 1)
	{
		t_value = document.forms[0].domainsel.options[idx].value;
		document.forms[0].domainsel.options[idx].value = document.forms[0].domainsel.options[idx - 1].value;
		document.forms[0].domainsel.options[idx].text = document.forms[0].domainsel.options[idx - 1].text;
		document.forms[0].domainsel.options[idx - 1].value = t_value;
		document.forms[0].domainsel.options[idx - 1].text = t_value;
		document.forms[0].domainsel.selectedIndex --;
		refresh_domaincache();
	}
}

function down_domain()
{
	var idx = document.forms[0].domainsel.selectedIndex;

	if ( (idx < document.forms[0].domainsel.options.length - 1)&&(idx >=0))
	{
		t_value = document.forms[0].domainsel.options[idx].value;
		document.forms[0].domainsel.options[idx].value = document.forms[0].domainsel.options[idx + 1].value;
		document.forms[0].domainsel.options[idx].text = document.forms[0].domainsel.options[idx + 1].text;
		document.forms[0].domainsel.options[idx + 1].value = t_value;
		document.forms[0].domainsel.options[idx + 1].text = t_value;
		document.forms[0].domainsel.selectedIndex ++;
		refresh_domaincache();
	}
}

function refresh_domaincache()
{
	var str ="";
	for (i = 0; i < document.forms[0].domainsel.options.length; i++)
	{
		str += (document.forms[0].domainsel.options[i].value + ":");
		//if (i < (document.forms[0].hostlist.options.length -1))
		//	str +=":";
	}
	document.forms[0].domaincache.value = str;
}

function add_host(n)
{
	document.forms[0].hostscache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].hostrdn.length; i++)
		{
			document.forms[0].hostscache.value +=(document.forms[0].ipin[i].value +"="+
							document.forms[0].hostin[i].value + ":");
		}
	}
	else if (n == 1)
	{
		document.forms[0].hostscache.value +=(document.forms[0].ipin.value +"="+
							document.forms[0].hostin.value + ":");
	}
	document.forms[0].hostscache.value +=(document.forms[0].ipaddin.value +"="+
							document.forms[0].hostaddin.value);

	document.forms[0].ipaddin.value = "";
	document.forms[0].hostaddin.value = "";
	document.forms[0].action = "index.cgi";
	document.forms[0].submit();
	disable_buttons();
}

function remove_host(n)
{
	var found = -1;
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].hostrdn.length; i++)
		{
			if (document.forms[0].hostrdn[i].checked)
			{
				found = i;
				break;
			}
		}
	}
	else if (n == 1)
	{
		if (document.forms[0].hostrdn.checked)
		{
			found = 0;
		}
	}
	if (found >= 0)
	{
		document.forms[0].hostscache.value = "";
		if (n > 1)
		{
			for (i = 0; i < document.forms[0].hostrdn.length; i++)
			{
				if (i != found)
					document.forms[0].hostscache.value +=(document.forms[0].ipin[i].value +"="+
							document.forms[0].hostin[i].value + ":");
			}
		}
		document.forms[0].action = "index.cgi";
		document.forms[0].submit();
		disable_buttons();
	}
	else
	{
		alert("$text{'noselecthost'}");
	}
}

function cachehostlist(n)
{
	document.forms[0].hostscache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].hostrdn.length; i++)
		{
			document.forms[0].hostscache.value +=(document.forms[0].ipin[i].value +"="+
							document.forms[0].hostin[i].value);
			if (i < (document.forms[0].hostrdn.length - 1))
				document.forms[0].hostscache.value += ":";
		}
	}
	else if (n == 1)
	{
		document.forms[0].hostscache.value +=(document.forms[0].ipin.value +"="+
							document.forms[0].hostin.value);
	}
}

function refresh_submit()
{
	document.forms[0].action = 'save_global.cgi';
}

function disable_buttons()
{
	document.forms[0].adddnsbtn.disabled = true;
	document.forms[0].deldnsbtn.disabled = true;
	document.forms[0].updnsbtn.disabled = true;
	document.forms[0].downdnsbtn.disabled = true;
	document.forms[0].adddomainbtn.disabled = true;
	document.forms[0].deldomainbtn.disabled = true;
	document.forms[0].updomainbtn.disabled = true;
	document.forms[0].downdomainbtn.disabled = true;
	document.forms[0].addhost.disabled = true;
	document.forms[0].delhost.disabled = true;
	document.forms[0].savebtn.disabled = true;
}
</script>
EOF

#print "<hr>\n";
&footer("/", $text{'index'});
